package com.linkwechat.wecom.c.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkwechat.common.constant.Constants;
import com.linkwechat.common.core.domain.BaseEntity;
import com.linkwechat.common.core.domain.Tree;
import com.linkwechat.common.exception.wecom.WeComException;
import com.linkwechat.common.utils.SnowFlakeUtil;
import com.linkwechat.common.utils.TreeUtil;
import com.linkwechat.wecom.c.service.ICategoryService;
import com.linkwechat.wecom.domain.WeCategory;
import com.linkwechat.wecom.domain.vo.WeCategoryVo;
import com.linkwechat.wecom.mapper.WeCategoryMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author oweson
 * @date 2022/2/17 22:47
 */

@Service
public class WeCategoryServiceImpl extends ServiceImpl<WeCategoryMapper, WeCategory> implements ICategoryService {
    @Override
    public void insertCategory(WeCategory category) {
        // 判断是否存在相同的名称
        WeCategory weCategory = this.getOne(new LambdaQueryWrapper<WeCategory>()
                .eq(WeCategory::getMediaType, category.getMediaType())
                .eq(WeCategory::getName, category.getName())
                .eq(WeCategory::getDelFlag, Constants.NORMAL_CODE));
        if (weCategory != null) {
            throw new WeComException("名称已经存在！");
        }
        category.setId(SnowFlakeUtil.nextId());
        this.save(category);

    }

    @Override
    public void updateCategory(WeCategory category) {
        //判断是否存在相同的名称
        WeCategory weCategory = this.getOne(new LambdaQueryWrapper<WeCategory>()
                .eq(WeCategory::getMediaType, category.getMediaType())
                .eq(WeCategory::getName, category.getName())
                .eq(WeCategory::getDelFlag, Constants.NORMAL_CODE)
                .ne(WeCategory::getId, category.getId()));

        if (null != weCategory) {
            throw new WeComException("名称已存在！");
        }
        this.updateById(category);

    }

    @Override
    public void deleteWeCategory(Long[] ids) {
        this.baseMapper.deleteWeCategoryById(ids);

    }

    @Override
    public List<? extends Tree> findWeCategoryByMediaType(String mediaType) {
        List<WeCategory> weCategories = this.list(new LambdaQueryWrapper<WeCategory>()
                .eq(WeCategory::getMediaType, mediaType)
                .eq(WeCategory::getDelFlag, Constants.NORMAL_CODE)
                .orderByDesc(BaseEntity::getCreateTime));
        List<WeCategoryVo> categoryVoList = weCategories.stream().map(weCategory -> {
            WeCategoryVo weCategoryVo = new WeCategoryVo();
            weCategoryVo.setFlag(weCategory.getFlag());
            weCategoryVo.setId(weCategory.getId());
            weCategoryVo.setName(weCategory.getName());
            weCategoryVo.setParentId(weCategoryVo.getParentId());
            return weCategoryVo;
        }).collect(Collectors.toList());

        return TreeUtil.build(categoryVoList);

    }
}
